﻿using System.Collections.Generic;

namespace SSS.AlphaNN.Forms
{
    using System;
    using System.IO;
    using System.Windows.Forms;

    using Components;

    public partial class PropertiesFOForm : PropertiesForm
    {
        private Sink _sender;

        private TypeFileInput typeTileInputForThisForm;
        
        public PropertiesFOForm(Sink sender)
        {
            InitializeComponent();
            _sender = sender;
            switch (sender.typeFileInput)
            {
                case TypeFileInput.Access:
                  
                    typeTileInputForThisForm = TypeFileInput.Access;
                    _textBox1.Text = _sender.Login ?? "";
                    _textBox2.Text = _sender.Password ?? "";
                    pathDB.Text = _sender.PathFileDb ?? "";
                    checkBox1.Checked = true;
                    checkBox1_CheckedChanged(checkBox1, new EventArgs());
                  
                    if ((_sender.Login ?? "").Length > 0)
                    {
                        checkBox2.Checked = true;
                    }
                    break;
                case TypeFileInput.CSV:
                  
                    pathDB.Text = _sender.PathFileDb??"";
                    typeTileInputForThisForm = TypeFileInput.CSV;
                    //if (_sender.columnNames != null)
                    //{
                    //    _sender.columnNames.ForEach(x => nameColumns.Text += x + " ;");
                    //    nameColumns.Text = nameColumns.Text.TrimEnd(new char[] {';'});
                    //}
                    break;
                case TypeFileInput.XML:
                    
                    pathDB.Text = _sender.PathFileDb??"";
                    typeTileInputForThisForm = TypeFileInput.XML;
                    //if (_sender.columnNames != null)
                    //{
                    //    _sender.columnNames.ForEach(x => nameColumns.Text += x + " ;");
                    //    nameColumns.Text = nameColumns.Text.TrimEnd(new char[] {';'});
                    //}
                    break;
                default:
                    MessageBox.Show("А такое вообще возможно? А? Как сделал?",
                                    "Ошибка в типе файле для выходного сигнала", MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                    break;

            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                OpenFileDialog ofd=new OpenFileDialog();
                ofd.Filter = "mdb Files|*.mdb|accdb Files|*accdb";
                
                if (ofd.ShowDialog()==DialogResult.OK)
                {
                    typeTileInputForThisForm = TypeFileInput.Access;
                    pathDB.Text = ofd.FileName;
                    _sender.PathFileDb = pathDB.Text;
                }
            }
            else
            {
                SaveFileDialog ofd = new SaveFileDialog();
                ofd.Filter = "*.csv | *.csv | *.xml | *.xml ";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    string filename = ofd.FileName;
                    _sender.PathFileDb = filename;
                    if (Path.GetExtension(filename).IndexOf("xml") > -1)
                    {
                        typeTileInputForThisForm = TypeFileInput.XML;
                       
                        pathDB.Text = ofd.FileName;
                        
                    }

                    else
                    {
                        if (Path.GetExtension(filename).IndexOf("csv") > -1)
                        {
                            typeTileInputForThisForm = TypeFileInput.CSV;
                            
                            pathDB.Text = ofd.FileName;
                        }
                        else
                        {
                            MessageBox.Show("Ошибка! Неверный формат файла!", "Ошибка", 
                                MessageBoxButtons.OK,
                                            MessageBoxIcon.Error);
                        }
                    }
                }    
            }
            
        }


        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            //Если мы выбираем созранение в базу данных то активными становятся некоторые компоненты
            bool b = checkBox1.Checked & checkBox2.Checked;
            Password.Enabled = b;
            nameUserLabel.Enabled = b;
            _textBox1.Enabled = b;
            _textBox2.Enabled = b;
            checkBox2.Enabled = checkBox1.Checked;
            //checkBox2_CheckedChanged
          
          
            typeTileInputForThisForm = TypeFileInput.Access;
           
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            //если есть пароль к базе данных
            Password.Enabled = checkBox2.Checked;
            nameUserLabel.Enabled = checkBox2.Checked;
            _textBox1.Enabled = checkBox2.Checked;
            _textBox2.Enabled = checkBox2.Checked;
           
            if (!checkBox1.Checked)
            {
                _textBox1.Text = String.Empty;
                _textBox2.Text = String.Empty;
            }

        }

        protected override void SetProperties()
        {
            switch (typeTileInputForThisForm)
            {
                case TypeFileInput.Access:
                    if (checkBox1.Checked)
                    {
                        _sender.Login = _textBox1.Text;
                        _sender.Password = _textBox2.Text;
                    }
                    else
                    {
                        _sender.Login = String.Empty;
                        _sender.Password = String.Empty;
                    }
                   
                    _sender.typeFileInput = TypeFileInput.Access;
                    //_sender.columnNames = new List<string>(nameColumns.Text.Split(new char[]{';'}, StringSplitOptions.RemoveEmptyEntries));
                    _sender.PathFileDb = pathDB.Text;
                    break;
                case TypeFileInput.CSV:

                    _sender.PathFileDb = pathDB.Text;
                    _sender.typeFileInput = TypeFileInput.CSV;
                    //_sender.columnNames = new List<string>(nameColumns.Text.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries));
                    break;
                case TypeFileInput.XML:

                    _sender.PathFileDb = pathDB.Text;
                    _sender.typeFileInput = TypeFileInput.XML;
                    //_sender.columnNames = new List<string>(nameColumns.Text.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries));
                    break;
                default:
                    MessageBox.Show("А такое вообще возможно? А? Как сделал?",
                                    "Ошибка в типе файле для выходного сигнала", MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                    break;

            }
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SetProperties();
            Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SetProperties();
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            Close();
        }
    }
}
